#include <iostream>
#include <cstdio>
#include <memory>

using namespace std; 

struct map
{
	int Gap; 
	int Board[9];
}

const int max_len = 20000; 
const int hash_size = 59999; 

int SLen, DLen, SCursor, DCursor, SStep, DStep; 
int SQ[max_len], DQ[max_len]; 
char SGaps[max_len], DGaps[max_len]; 

char Hash[hash_size]; 
int HashKeys[hash_size];
map SMap, DMap; 
int SKey, DKey; 

inline char & hash (int V)
{
	int Key = V * 4 % hash_size; 
	V++; 
	while (HashKey[Key] != 0 && HashKeys[Key] != V)
	{
		Key++; 
		if (Key == hash_size) Key = 0;
	}
	HashKey[Key] = V; 
	return Hash[Key];
}

inline int convert(int P[])
{
	int I; 
	int Key = 0; 
	for (I = 8; I >= 0; --I)
	{
		if (P[I] != 0)
		{
			Key <<= 3; 
			Key |= P[I] - 1;
		}
	}
}



void read_map(map & Map)
{
	int I; 
	for (I = 0; I < 9; I++)
	{
		scanf(" %d", &Map.Board[I]);
		if (Map.Board[I] == 0) Map.Gap = I;
	}
}

bool search(int & FCursor, int & FLen, int Q[], char Gaps[], int Dir)
{
	int Cursor = FCursor; 
	int Len = FLen; 
	int Limit = Len; 
	whie (Cursor < Limit)
	{
		int Key = Q[Cursor];
		char Gap = Gaps[Cursor];
		int Delta = Gap + Gap + Gap; 
		
		if (Gap < 6)
		{
			// down
			int Key2 = Key & ~(511 << Delta) | ((Key & (63<< Delta)) << 3) | ((Key & (7 << (Delta + 6))) >> 6); 
			char Gap2 = Gap + 3;
			char &H = hash(Key2 * 9 + Gap2);
			if ((H & Dir) != Dir)
				if ((H |= Dir) == 3) return true; 
				else 
				{
					Q[Len] = Key2; 
					Gaps[Len++] = Gap2;
				}
		}

		if (Gap >= 3)
		{
			// up
			int Key2 = Key & ~(511 << (Delta - 9)) | ((Key & (63 << (Delta - 6))) >> 3) | ((Key & (7 << (Delta - 9))) << 6);
			char Gap2 = Gap - 3;
			char & H = hash(Key2 * 9 + Gap2);
			if ((H & Dir) != Dir)
				if ((H |= Dir) == 3) return true;
				else 
				{
					Q[Len] = Key2;
					Gaps[Len++] = Gap2;
				}
		}
		if (Gap != 0 && Gap != 3 && Gap != 6)
		{
			// left
			int Key2 = Key; 
			char Gap2 = Gap - 1;
			char& H = hash(Key2 * 9 + Gap2);
			if ((H & Dir) != Dir)
				if ((H |= Dir) == 3) return true;
				else 
				{
					Q[Len] = Key2;
					Gaps[Len++] =Gap2;
				}
		}

		if (Gap != 2 && Gap != 5 && Gap != 8)
		{
			int Key2 = Key; 
			char Gap2 = Gap + 1; 
			char & H = hash(Key2 * 9 + Gap2);

			if ((H & Dir) != Dir)
				if ((H & Dir) == 3) return true;
				else 
				{
					Q[Len] = Key2;
					Gaps[Len++] = Gap2; 
				}
		}
		Cursor++;
	}
	FCursor = Cursor; 
	FLen = Len; 
	return false;
}


void perform()
{
	freopen("start.txt", "r", stdin);
	read_map(SMap);
	freopen("goal.txt", "r", stdin);
	read_map(DMap);

	SKey = convert(SMap.Board);
	DKey = convert(DMap.Board);
	
	if (SMap.Gap == DMap.Gap && SKey == DKey)
	{
		printf("0\n");
		return ;
	}

	hash(SKey * 9 + SMap.Gap) |= 1; 
	hash(DKey * 9 + DMap.Gap) |= 2;

	SLen = SCursor = DLen = DCursor = 0; 

	SQ[SLen] = SKey; 
	SGap[SLen ++] = SMap.Gap; 
	
	DQ[Dlen] = DKey; 
	DGap[DLen++] = DMap.Gap; 
	
	SStep = 0; 
	DStep = 0;

	while (SStep < 16 || DStep < 16)
	{
		if (SCursor < SLen && DCursor < DLen && SStep <= DStep || DCursor >= DLen && SCursor < SLen)
		{
			if (search(SCursor, SLen, SQ, SGaps, 1))
			{
				printf("%d\n", SStep + DStep + 1);
				return ;
			}
			else 
				SStep++;
		}
		else if (DCursor < DLen)
		{
			if (search(DCursor, DLen, DQ, DGaps, 2))
			{
				printf("%d\n", SStep + DStep + 1);
				return ;
			}
			else 
				DStep++;
		}
		else 
			break; 
	}
	printf("-1\n");
}


int main()
{
	perform();
	return 0; 
}